﻿using Student2024.Model.ViewModel;
using Student2024.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Student2024.UI.form
{
    public partial class FormEdit : Form
    {
        /// <summary>
        /// 获取当前记录的id
        /// </summary>
        private Guid id;
        private string examType;
        private readonly StudentSystemService service = new StudentSystemService();

        /// <summary>
        /// 构造函数，初始化并加载当前记录的数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="ExamType"></param>
        public FormEdit(Guid id,string ExamType)
        {
            InitializeComponent();
            var item =service.GetById(id);//从服务层获取当前数据
            if (item != null)
            {
                //将数据填充到窗体中的控件里面
                txtName.Text = item.Name;
                comboBoxProvince.Text = item.Province;
                comboBoxObjectGroup.Text = item.ObjectGroup;
                txtSchoolName.Text = item.SchoolName;
                txtPhoneNum.Text = item.PhoneNum;
                comboBoxGender.Text = item.Genger;
                this.id = id;//设置当前记录的id
                this.examType = ExamType;//设置高考类型
            }
            //按照高考类型分类显示学科组
            if (ExamType == "新高考")
            {
                comboBoxObjectGroup.Items.Add("物理组");
                comboBoxObjectGroup.Items.Add("历史组");
            }
            else if (ExamType == "老高考")
            {
                comboBoxObjectGroup.Items.Add("理科");
                comboBoxObjectGroup.Items.Add("文科");
            }
        }
        /// <summary>
        /// 取消操作，关闭对话框，返回取消结果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
        /// <summary>
        /// 保存操作，更新当前记录的数据并关闭对话框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            //修改学生记录并填充数据
            T001StudentEditModel model = new T001StudentEditModel()
            {
                Id = this.id,
                Province = comboBoxProvince.Text,
                ExamType = examType,
                ObjectGroup = comboBoxObjectGroup.Text,
                SchoolName = txtSchoolName.Text,
                PhoneNum = txtPhoneNum.Text,
                Genger = comboBoxGender.Text,
                Name = txtName.Text,
            };
            //调用服务层方法更新记录
            service.Edit(model);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
